package yao.spider.jsoup.vobao;


import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;

import yao.spider.jsoup.common.MongoUtils;
import yao.spider.jsoup.vobao.bean.Answer;
import yao.spider.jsoup.vobao.bean.Question;

public class ResultHandler {
	
	private static final ResultHandler instance = new ResultHandler();
	
	private static final DBCollection collection = MongoUtils.getCollection("spider_vobao"); 
	public void exec(Question question) {
		if(question == null) {
			return;
		}
		collection.insert(convert(question));		
	}
	
	private DBObject convert(Question question) {
		BasicDBList answerDBList = new BasicDBList();
		for(Answer answer : question.getAnswers()) {
			DBObject answerDb = new BasicDBObject()
					.append("content", answer.getContent())
					.append("answerTime", answer.getAnswerTime());
			
			answerDBList.add(answerDb);
		}
		
		DBObject dbObject = new BasicDBObject()
				.append("title", question.getTitle())
				.append("url", question.getUrl())
				.append("category", question.getCategory())
				.append("answers", answerDBList)
				.append("tags", question.getTags());
		return dbObject;
	}
	
	public static ResultHandler getInstance() {
		return instance;
	}

}
